package com.ytjj.quartz.service;

import com.ytjj.common.constants.StatisticsConstants;
import com.ytjj.qmyx.mall.mapper.ChannelMapper;
import com.ytjj.qmyx.mall.mapper.DataAnalyseMapper;
import com.ytjj.qmyx.mall.mapper.DataModelAnalyseMapper;
import com.ytjj.quartz.dao.DataAnalyseDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Map;

/**
 *
 * @version 1.0
 * 
 */

@Service
@Slf4j
public class DataAnalyseService {

    @Resource
    private RedisTemplate redisTemplate;

    public void statistics() {
        LocalDate localDate = LocalDate.now().minusDays(1);
        Map channelMap = redisTemplate.opsForHash().entries(StatisticsConstants.SKIP_NUM + localDate);
        for (Object channelName : channelMap.keySet()) {
            String channel = channelName.toString();
            // 重定向数
            Integer.parseInt(channelMap.get(channel).toString());
        }

    }


    public void dataAnalyseStatistics(String startDate, String endDate) {
//
//        // 取出所有渠道
//        ChannelExample channelExample = new ChannelExample();
//        channelExample.createCriteria()
//                .andStatusEqualTo((byte) 1);
//        List<Channel> channelList = channelMapper.selectByExample(channelExample);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String createDate = sdf.format(new Date());
////        // 遍历所有渠道
//        channelList.stream().filter(item -> !item.getChannel().equals("channel-arr"))
//                .forEach(item -> {
//            // 判断渠道数据是否插入过
//            DataAnalyse dataAnalyse = dataAnalyseDao.queryDataAnalyseByCreateDate(createDate, item.getChannel());
//            // 重定向数
//            Integer countRedirect = dataAnalyseDao.countRedirectByChannel(item.getChannel(), startDate, endDate,null);
//            // uv
//            Integer countSum = dataAnalyseDao.countSum(item.getChannel(), startDate, endDate, null,null);
//            // 订单数
//            Integer orderNum = dataAnalyseDao.countOrderNum(item.getChannel(), startDate, endDate, null,null);
//            // 无地址订单数
//            Integer noAddressOrder = dataAnalyseDao.countNoAddressOrder(item.getChannel(), startDate, endDate, null, null);
//            // 真实订单数
//            Integer realOrderNum = orderNum - noAddressOrder;
//            // 支付订单数
//            Integer payOrderNum = dataAnalyseDao.countPayOrderNum(item.getChannel(), startDate, endDate, null,null);
//            // 订单率
//            BigDecimal orderRate = countSum == 0 ? new BigDecimal(0) : BigDecimal.valueOf(realOrderNum, 2)
//                    .divide(BigDecimal.valueOf(countSum, 2), 4, RoundingMode.HALF_UP);
//            // 支付率
//            BigDecimal payRate = realOrderNum == 0 ? new BigDecimal(0) : BigDecimal.valueOf(payOrderNum,2)
//                    .divide(BigDecimal.valueOf(realOrderNum,2), 4, RoundingMode.HALF_UP);
//
//            if (null == dataAnalyse) {
//                // 没有插入过渠道数据
//                dataAnalyse = new DataAnalyse();
//                dataAnalyse.setChannel(item.getChannel());
//                dataAnalyse.setCreateTime(createDate);
//                dataAnalyse.setStatisticsTime(new Date());
//                dataAnalyse.setRedirect(countRedirect);
//                dataAnalyse.setUv(countSum);
//                dataAnalyse.setOrderNum(orderNum);
//                dataAnalyse.setNoAddressOrder(noAddressOrder);
//                dataAnalyse.setRealOrderNum(realOrderNum);
//                dataAnalyse.setPayOrderNum(payOrderNum);
//                dataAnalyse.setOrderRate(orderRate);
//                dataAnalyse.setPayRate(payRate);
//                dataAnalyseMapper.insertSelective(dataAnalyse);
//            } else {
//                dataAnalyse.setChannel(item.getChannel());
//                dataAnalyse.setStatisticsTime(new Date());
//                dataAnalyse.setRedirect(countRedirect);
//                dataAnalyse.setUv(countSum);
//                dataAnalyse.setOrderNum(orderNum);
//                dataAnalyse.setNoAddressOrder(noAddressOrder);
//                dataAnalyse.setRealOrderNum(realOrderNum);
//                dataAnalyse.setPayOrderNum(payOrderNum);
//                dataAnalyse.setOrderRate(orderRate);
//                dataAnalyse.setPayRate(payRate);
//                dataAnalyseMapper.updateByPrimaryKeySelective(dataAnalyse);
//            }
//            DataModelEnums[] dataModelEnums = DataModelEnums.values();
//            // 查询渠道的数据分析插入
//            // 查询渠道模版数据分析插入
//            Arrays.asList(dataModelEnums).stream().forEach(modelItem -> {
//                String model = modelItem.name();
//                DataModelAnalyse dataModelAnalyse = dataAnalyseDao.queryDataModelAnalyseByCreateDate(item.getChannel(), model, createDate);
//                // uv
//                Integer countSumModel = dataAnalyseDao.countSum(item.getChannel(), startDate, endDate, modelItem.getPathCode(),null);
//                // 订单数
//                Integer orderNumModel = dataAnalyseDao.countOrderNum(item.getChannel(), startDate, endDate, model,null);
//                // 无地址订单数
//                Integer noAddressOrderModel = dataAnalyseDao.countNoAddressOrder(item.getChannel(), startDate, endDate, model,null);
//                // 真实订单数
//                Integer realOrderNumModel = orderNumModel - noAddressOrderModel;
//                // 支付订单数
//                Integer payOrderNumModel = dataAnalyseDao.countPayOrderNum(item.getChannel(), startDate, endDate, model,null);
//                // 订单率
//                BigDecimal orderRateModel = countSumModel == 0 ? new BigDecimal(0) : BigDecimal.valueOf(realOrderNumModel,2)
//                        .divide(BigDecimal.valueOf(countSumModel,2) ,4, BigDecimal.ROUND_HALF_UP);
//                // 支付率
//                BigDecimal payRateModel = realOrderNumModel == 0 ? new BigDecimal(0) : BigDecimal.valueOf(payOrderNumModel,2)
//                        .divide(BigDecimal.valueOf(realOrderNumModel,2),4, BigDecimal.ROUND_HALF_UP);
//
//                if (null == dataModelAnalyse) {
//                    dataModelAnalyse = new DataModelAnalyse();
//                    dataModelAnalyse.setChannel(item.getChannel());
//                    dataModelAnalyse.setCreateTime(createDate);
//                    dataModelAnalyse.setStatisticsTime(new Date());
//                    dataModelAnalyse.setModel(modelItem.name());
//                    dataModelAnalyse.setUv(countSumModel);
//                    dataModelAnalyse.setOrderNum(orderNumModel);
//                    dataModelAnalyse.setNoAddressOrder(noAddressOrderModel);
//                    dataModelAnalyse.setRealOrderNum(realOrderNumModel);
//                    dataModelAnalyse.setPayOrderNum(payOrderNumModel);
//                    dataModelAnalyse.setOrderRate(orderRateModel);
//                    dataModelAnalyse.setPayRate(payRateModel);
//                    dataModelAnalyseMapper.insertSelective(dataModelAnalyse);
//                } else {
//                    dataModelAnalyse.setChannel(item.getChannel());
//                    dataModelAnalyse.setCreateTime(createDate);
//                    dataModelAnalyse.setStatisticsTime(new Date());
//                    dataModelAnalyse.setModel(modelItem.name());
//                    dataModelAnalyse.setUv(countSumModel);
//                    dataModelAnalyse.setOrderNum(orderNumModel);
//                    dataModelAnalyse.setNoAddressOrder(noAddressOrderModel);
//                    dataModelAnalyse.setRealOrderNum(realOrderNumModel);
//                    dataModelAnalyse.setPayOrderNum(payOrderNumModel);
//                    dataModelAnalyse.setOrderRate(orderRateModel);
//                    dataModelAnalyse.setPayRate(payRateModel);
//                    dataModelAnalyseMapper.updateByPrimaryKeySelective(dataModelAnalyse);
//                }
//            });
//        });


    }
}
